package com.itnature.testchip.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.itnature.testchip.business.HeaderFooter;
import com.itnature.testchip.business.StopBin;
import com.itnature.testchip.business.TFDoc;
import com.itnature.testchip.business.TestSuite;
import com.itnature.testchip.business.Timing;
import com.itnature.testchip.business.VDDDetail;
import com.itnature.testchip.business.VDDSummary;
import com.itnature.testchip.process.ApplicationUtil;
import com.itnature.testchip.process.ExcelFileProcessing;
import com.itnature.testchip.process.TestChipProcessing;

public class TestChipFrame extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btn1;
	private JButton btn3;
	private JButton btn2;
	private JButton btn4;
	private JButton btn5;
	private JButton btn6;
	private JTextField tf4;
	private JTextField tf2;
	private JTextField tf0;
	private JTextField tf1;
	private JTextField tf3;
	
	private String currentPath = null;

	public TestChipFrame(String tittle) {
		super(tittle);

		addComponent();
		registerListener();

		setSize(480, 260);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	//add cac component vao container
	public void addComponent() {
		setLayout(null);
		
		JLabel text = createLabel("Test Program Release Documentation",120,8,240,20);
		add(text);
		int y0 = 20,x0 = 30 ,x1=130, x2 = 340;
		
		text = createLabel("Date: ",x0,25 + y0,100,20);
		add(text);
		tf0 = createTextField(x1, y0+25 , 200, 20);
		Date d = new Date();
		tf0.setText(TestChipProcessing.format(d));
		add(tf0);
		
		text = createLabel("Test flow file: ",x0 ,50 + y0,100,20);
		add(text);
		tf1 = createTextField(x1, y0+50 , 200, 20);
		tf1.setEditable(false);
		add(tf1);
		btn1 = createButton("Browse", x2, y0 + 50, 80, 20);
		btn1.addActionListener(this);
		add(btn1);
		
		text = createLabel("Level file: ",x0,75 + y0,100,20);
		add(text);
		tf2 = createTextField(x1, y0+100 , 200, 20);
		tf2.setEditable(false);
		add(tf2);
		btn2 = createButton("Browse", x2, y0 + 100, 80, 20);
		btn2.addActionListener(this);
		add(btn2);
		
		text = createLabel("Timing file: ",x0,100 + y0,100,20);
		add(text);
		tf3 = createTextField(x1, y0+75 , 200, 20);
		tf3.setEditable(false);
		add(tf3);
		btn3 = createButton("Browse", x2, y0 + 75, 80, 20);
		btn3.addActionListener(this);
		add(btn3);
		
		text = createLabel("Output file: ",x0,125 + y0,100,20);
		add(text);
		tf4 = createTextField(x1, y0+125 , 200, 20);
		tf4.setEditable(false);
		add(tf4);
		btn4 = createButton("Browse", x2, y0 + 125, 80, 20);
		btn4.addActionListener(this);
		add(btn4);
		
		int x = x1 +110;
		btn5 = createButton("Generate", x, y0 + 170 ,90, 20);
		btn5.addActionListener(this);
		add(btn5);
		btn6 = createButton("Exit", x + 100, y0 + 170, 80, 20);
		btn6.addActionListener(this);
		add(btn6);
	}

	private JTextField createTextField(int x,int y, int w, int h) {
		JTextField tf = new JTextField();
		setPosition(tf, x, y, w, h);
		return tf;
	}
	
	private JButton createButton(String text,int x,int y, int w, int h) {
		JButton btn = new JButton(text);
		setPosition(btn, x, y, w, h);
		return btn;
	}
	
	private JLabel createLabel(String text,int x,int y, int w, int h) {
		JLabel lbl = new JLabel(text);
		setPosition(lbl,x, y, w, h);
		return lbl;
	}

	private void setPosition(JComponent lbl,int x, int y, int w, int h) {
		lbl.setSize(w, h);
		lbl.setLocation(x, y);
	}

	//dang ki cac su kien cho cac control
	public void registerListener() {

	}
	

	public void actionPerformed(ActionEvent arg0) {
		JButton source = (JButton) arg0.getSource();
		if(source == btn6) {
			System.exit(0);
			return;
		}
		if(source == btn5) {
			String date = tf0.getText();
			String tfFile = tf1.getText();
			String levelFile = tf2.getText();
			String timingFile = tf3.getText();
			String out = tf4.getText();
			try {
				generate(date,tfFile,levelFile,timingFile,out);
				JOptionPane.showMessageDialog(this,"Generate file successfully","Success",JOptionPane.INFORMATION_MESSAGE);
			} catch (FileNotFoundException e) {
				JOptionPane.showMessageDialog(this, "File not found","Error",JOptionPane.ERROR);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		if(source == btn4) {
			chooseFilePath(tf4,JFileChooser.SAVE_DIALOG);
			return;
		}
		if(source == btn2) {
			chooseFilePath(tf2,JFileChooser.OPEN_DIALOG);
			return;
		}
		if(source == btn3) {
			chooseFilePath(tf3,JFileChooser.OPEN_DIALOG);
			return;
		}
		if(source == btn1) {
			chooseFilePath(tf1,JFileChooser.OPEN_DIALOG);
			return;
		}
	}

	private void chooseFilePath(JTextField tf, int type) {
		if(currentPath == null) {
			currentPath = "c:\\";
		}
		JFileChooser dlg = new JFileChooser(currentPath);
		int rs ;
		if(type == JFileChooser.OPEN_DIALOG) {
			rs = dlg.showOpenDialog(this);
		}else {
			rs = dlg.showSaveDialog(this);
		}
		
		if(rs == JFileChooser.APPROVE_OPTION) {
			String path = dlg.getSelectedFile().getPath();
			currentPath = path.substring(0,path.lastIndexOf("\\"));
			tf.setText(path);
		}
	}

	private void generate(String date, String tfFile, String timingFile,
			String levelFile, String outFile) throws Exception{
		try {
			TFDoc tfDoc = new TFDoc();
			
			String tfContent = ApplicationUtil.readFile(tfFile);
			File f = new File(tfFile);
			//header footer
			HeaderFooter headerFooter = TestChipProcessing.getHeaderFooter(tfContent, f.getName());
			headerFooter.setDate(date);
			tfDoc.setHeaderFooter(headerFooter);
			//stopbin
			Vector<StopBin> stopBins = TestChipProcessing.getStopBins(tfContent);
			tfDoc.setStopBin(stopBins);
			//testsuite
			Vector<TestSuite> testSuites = TestChipProcessing.getTestSuites(tfContent,stopBins);
			tfDoc.setTestSuite(testSuites);
			
			//calculate timing from test suites
			tfContent = ApplicationUtil.readFile(timingFile);
			//timing
			Vector<Timing> timings = TestChipProcessing.getTimings(tfContent,testSuites);
			//calculate vdd is in next step
			tfContent = ApplicationUtil.readFile(levelFile);
			timings = TestChipProcessing.getVdd(tfContent,testSuites,timings);
			tfDoc.setTiming(timings);
		
			ExcelFileProcessing.createUniqueInstance(outFile);
			ExcelFileProcessing.get().buildTFDOCSheet(tfDoc);

			Vector<VDDSummary> vddSummaries = TestChipProcessing.getVddSummary(testSuites,timings);
			Vector<VDDDetail> vddDetail = TestChipProcessing.getVddDetail(tfContent,vddSummaries);
			
			ExcelFileProcessing.get().buildVDDSheet(vddSummaries,vddDetail);
			ExcelFileProcessing.get().flush();
		} catch (Exception e) {
			Logger.getLogger(this.getClass().getName()).debug(e);
		}
	}
	
	/////////////////////////////////////////////////////////
	//
	private static void initLog4j() {
		PropertyConfigurator.configure("log4j.properties");		
	}
	public static void main(String[] args) {
		initLog4j();
		(new TestChipFrame("Test flow reader")).setVisible(true);
	}
}